package com.loong.web.gp4j.service.user;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.loong.tool.lang.Strings;
import com.loong.web.gp4j.bean.param.messages.MessagePublishParam;
import com.loong.web.gp4j.bean.param.users.UserParam;
import com.loong.web.gp4j.bean.schema.message.UserMessageSchema;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.bean.schema.user.UserAuthSchema;
import com.loong.web.gp4j.bean.schema.user.UserDetailSchema;
import com.loong.web.gp4j.bean.schema.user.UserSchema;
import com.loong.web.gp4j.bean.vo.user.UserVo;
import com.loong.web.gp4j.bean.vo.util.CodeVo;
import com.loong.web.suppert.auth.User;
import com.loong.web.suppert.collection.list.KvnList;

/**
 * 用户封装器
 *
 * @author 张成轩
 */
public class UserAdapter {

	/**
	 * 构建用户Session对象
	 * 
	 * @param schema
	 * @param list
	 * @return 用户Session对象
	 */
	public static User build(UserSchema schema, KvnList<String, Integer, String> list) {

		Set<String> auths = new HashSet<String>();
		if (schema.getAuths() != null)
			for (UserAuthSchema auth : schema.getAuths())
				auths.add(list.getK(auth.getAuth()));
		auths.add(Auths.USER);
		return new User(schema.getId(), schema.getUser(), auths);
	}

	/**
	 * @param schemas
	 * @param auths
	 * @param statuses
	 * @param sexs
	 * @param vos
	 */
	public static void convert(List<UserSchema> schemas, KvnList<String, Integer, String> auths,
			KvnList<String, Integer, String> statuses, Map<Integer, String> sexs, List<UserVo> vos) {

		for (UserSchema schema : schemas) {

			UserVo vo = new UserVo();
			vos.add(vo);
			convert(schema, auths, statuses, sexs, vo);
		}
	}

	/**
	 * @param schema
	 * @param auths
	 * @param statuses
	 * @param sexs
	 * @param vo
	 */
	public static void convert(UserSchema schema, KvnList<String, Integer, String> auths,
			KvnList<String, Integer, String> statuses, Map<Integer, String> sexs, UserVo vo) {

		vo.setId(schema.getId());
		vo.setUser(schema.getUser());
		vo.setEmail(schema.getEmail());
		vo.setStatus(new CodeVo().kvn(statuses.getKvnByV(schema.getStatus())));
		UserDetailSchema userDetail = schema.getDetail();
		if (userDetail != null) {
			vo.setName(userDetail.getName());
			vo.setBirthday(userDetail.getBirthday());
			if (userDetail.getSex() != null)
				vo.setSex(new CodeVo().value(userDetail.getSex()).name(sexs.get(userDetail.getSex())));
		}
		List<UserAuthSchema> userAuths = schema.getAuths();
		if (userAuths != null) {
			vo.setAuths(new LinkedList<CodeVo>());
			for (UserAuthSchema userAuth : userAuths)
				vo.getAuths().add(new CodeVo().kvn(auths.getKvnByV(userAuth.getAuth())));
		}
	}

	/**
	 * @param param
	 * @param schema
	 */
	public static void convert(UserParam param, UserSchema schema) {

		schema.setId(param.getId());
		schema.setUser(param.getUser());
		schema.setEmail(param.getEmail());
	}

	/**
	 * @param param
	 * @param schema
	 */
	public static void convert(UserParam param, UserDetailSchema schema) {

		schema.setName(Strings.isNull(param.getName()) ? null : param.getName());
		schema.setBirthday(Strings.isNull(param.getBirthday()) ? null : param.getBirthday());
		schema.setSex(param.getSex());
		schema.setUserId(param.getId());
	}

	/**
	 * @param param
	 * @param schemas
	 */
	public static void convert(UserParam param, List<UserAuthSchema> schemas) {

		if (param.getAuths() == null)
			return;
		for (Integer auth : param.getAuths()) {
			UserAuthSchema schema = new UserAuthSchema();
			schema.setUserId(param.getId());
			schema.setAuth(auth);
			schemas.add(schema);
		}
	}

	/**
	 * @param param
	 * @param users
	 * @param statuses
	 * @param messages
	 */
	public static void convert(MessagePublishParam param, List<UserSchema> users,
			KvnList<String, Integer, String> statuses, List<UserMessageSchema> messages) {

		for (UserSchema user : users) {

			UserMessageSchema message = new UserMessageSchema();
			messages.add(message);
			convert(param, user, message);
		}
	}

	/**
	 * @param param
	 * @param user
	 * @param message
	 */
	public static void convert(MessagePublishParam param, UserSchema user, UserMessageSchema message) {

		message.setUserId(user.getId());
		message.setMessageId(param.getId());
	}
}
